#!/bin/sh

####  This program collects some information about a system running MX
####  Non trivial bug reports should include the output file generated by this program
####   to ease/speed-up problem resolution diagnotic

export MX_DIR=@mx_dir@
PATH=$MX_DIR:$MX_DIR/../bin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH

if test "${FMS_RUN}" = ""; then
  FMS_RUN=@FMS_RUN@
fi

error ()
{
 echo >&2
 echo "ERROR:$@" >&2
 exit 1
}

delim ()
{
 set +x
 echo ""
 echo "***************"
 echo "***************"
 echo "** $@"
 echo "***************"
}

delimx ()
{
 set +x
 delim "$@"
 set -x
}

output="mx_bug.`hostname --short`-`date +%F_%H%m%S`.txt"
test "`id -nu`" = root || echo "**WARNING: not root user: report will be incomplete:please run as root**" >&2
echo "Will collect system info in file:" >&2
echo "     $output.gz  " >&2
echo " " >&2
echo "     Starting collecting system information..." >&2

(
exec > "$output" || error "cannot create $output file"
exec 2>&1
exec < /dev/null

###########################################
#Start collection various system information
###########################################

set -x
id -a
uname -a
set +x

for f in /etc/debian_version /etc/cat /etc/redhat-release /etc/SuSE-release; do
 if test -r $f ; then 
  delim $f
  cat "$f"
 fi
done

for f in /proc/cpuinfo /proc/meminfo /proc/version /proc/uptime  \
          /proc/cmdline /proc/interrupts /proc/net/dev /proc/iomem \
          /proc/mtrr; do
 delim "$f:"
 cat $f
done

delim "dmesg output"
dmesg

for logfile in /var/log/messages.1 /var/log/messages.0 /var/log/messages ; do
 if test -r $logfile  ; then
    delim "$logfile extract"
    egrep -A3 -B3 -i "mx|gm|lanai" "$logfile" | tail -2000
 fi
done

delim "lspci -vv -x"
lspci  -vv -x
delim "lspci -vv -xxx -d 14c1:"
lspci -vv -xxx -d 14c1:

if test -d /sys/modules/mx_driver/parameters; then
 delim "mx_driver params"
 find /sys/modules/mx_driver/parameters -type f | xargs head 
fi

delim "/dev/mx* info"
ls -l /dev/mx*
head /dev/mx*

delim "mx_info"
mx_info 2>&1

delim "mx_endpoint_info"
mx_endpoint_info

delimx "mx_counters -q -b 0"
mx_counters -q -b 0

bnum=0`mx_endpoint_info 2> /dev/null | head -1 | cut -d" " -f1`
bmax=`expr "$bnum" - 1`

for b in `seq 1 $bmax` ; do
   delim "mx_counters -q -b $b"
   mx_counters -q -b $b
done
set +x

delim "process info"
ps -ef

delimx "mapper files"
ls -l /var/run/mx_mapper/*
tail -100 /var/run/mx_mapper/err.* /var/run/mx_mapper/verbose.* 2>&1

cat /var/run/mx_mapper/map*.0

for b in `seq 1 $bmax` ; do
   delim "mx_mapper/map.$b"
   cat /var/run/mx_mapper/map*.$b
done
set +x

delimx "fma/fms files"
ls -l $FMS_RUN/.
tail -1000 $FMS_RUN/fm*log 2>&1

if test "$FMS_RUN" != /var/run/fms ; then
 ls -l /var/run/fms/.
 tail -1000 /var/run/fms/fm*log 2>&1
fi
set +x

delim "lsmod"
lsmod

if test -f /proc/config.gz ; then
  delim /proc/config.gz
  zcat /proc/config.gz
else
  delim "/boot/config-`uname -r`"
  cat /boot/config-`uname -r`
fi

for f in /var/log/dmesg /var/log/boot.msg; do
 if test -r $f ; then 
  delim $f
  cat "$f"
 fi
done

delim "sysctl info"
sysctl -a

delim "END OF BUG REPORT"

exit 0
) || error "collection failed"

### end of collection, compress file #####
echo >&2
rm -f "$output.gz"
gzip -9 "$output" || error "Cannot compress $output!?"
echo "" >&2
echo "    $output.gz created" >&2
echo "    Please send it to help@myri.com with a description of your problem" >&2
echo "" >&2
